package com.acme;

import java.io.Serializable;
import java.util.Date;

// TODO [MY] DebugListener class is not implemented.
public class Bank implements Serializable {

	private final Client[] clients = new Client[10];
	private int maxClients = 0;
	private ClientRegistrationListener[] listeners;

    private class PrintClientListener implements ClientRegistrationListener{
        public void onClientAdded(Client client){
            System.out.println(client);
        }
    }

    private class EmailNotificationListener implements ClientRegistrationListener{
        public void onClientAdded(Client client){
            Email notifyMail = new Email();
            notifyMail.setMassage("Welcome to the bank!");
            EmailService.sendMail(notifyMail);
            System.out.println("Notification email for client " + client.getSalutation() + " to be sent");
        }
    }

    private class DebugListener implements ClientRegistrationListener{
        public void onClientAdded(Client client){
            Date date = new Date();
            System.out.println(client + " " + date.toString() );
        }
    }

	public Bank(ClientRegistrationListener [] extraListeners) {
        ClientRegistrationListener [] bankListeners = new ClientRegistrationListener[extraListeners.length+3];
        bankListeners[0] = new PrintClientListener();
        bankListeners[1] = new EmailNotificationListener();
        bankListeners[2] = new DebugListener();
        System.arraycopy(extraListeners, 0, bankListeners, 3, extraListeners.length);
        listeners = bankListeners;
	}

	public Client addClient(final Client client) {
		clients[maxClients++] = client;

        for (ClientRegistrationListener listener: listeners){
            listener.onClientAdded(client);
        }

		return client;
	}

}
